package com.bhs.sansong.Receiver;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.bhs.sansong.bean.NewMessage;
import com.bhs.sansong.bean.NewNotice;
import com.bhs.sansong.bean.PushMessage;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import cn.jpush.android.api.CmdMessage;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

public class PushMessageReceiver extends JPushMessageReceiver {
    private static final String TAG = "hello";

    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        Log.e(TAG, "[onMessage] " + customMessage);
        Log.e(TAG, "[onMessage] " + customMessage.message);
        NewMessage NewMessage=new Gson().fromJson(customMessage.message,NewMessage.class);
        EventBus.getDefault().postSticky(NewMessage);
    }

    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageOpened] " + message);
        try{
            PushMessage pushMessage=new Gson().fromJson(message.notificationExtras, PushMessage.class);
        }catch (Throwable throwable){

        }
    }


    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageArrived] " + message);
        NewNotice NewMessage=new Gson().fromJson(message.notificationExtras, NewNotice.class);
        EventBus.getDefault().post(NewMessage);
    }

    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageDismiss] " + message);
    }

    @Override
    public void onRegister(Context context, String registrationId) {
        Log.e(TAG, "[onRegister] " + registrationId);
        Intent intent = new Intent("com.jiguang.demo.register");
        context.sendBroadcast(intent);
    }

    @Override
    public void onConnected(Context context, boolean isConnected) {
        Log.e(TAG, "[onConnected] " + isConnected);
    }

    @Override
    public void onCommandResult(Context context, CmdMessage cmdMessage) {
        Log.e(TAG, "[onCommandResult] " + cmdMessage);
    }


}
